package deweb.cimaplanning.beans;

import java.util.ArrayList;
import java.util.Hashtable;

import deweb.cimaplanning.dao.PODistributionListPerPlantDao;
import deweb.cimaplanning.dto.PODistributionListPerPlant;

public class PODistributionListPerPlantBean {
	protected boolean successful;
	protected String errorMsg;
	
	protected Hashtable elements;
	
	private PODistributionListPerPlantDao dao = null;
	
	public PODistributionListPerPlantBean(){
		dao = new PODistributionListPerPlantDao();
	}
	
	public ArrayList browse(){
		return browse(1, true, "");
	}
	
	public ArrayList browse(int orderField, boolean ascendingOrder, String filters){
		ArrayList results	= null;
		elements			= new Hashtable(100);
		
		PODistributionListPerPlant pod = null;
		
		try {
			results = dao.getArray(orderField, ascendingOrder, filters);
			
			for (int i = 0; i < results.size(); i++){
				pod = (PODistributionListPerPlant)results.get(i);
				elements.put("" + pod.getPurchaseOrderVersionId() + "|" + pod.getPlantId(), "1");
			}
			
			successful	= true;
			errorMsg	= "";
		} catch (Exception e) {
			successful	= false;
			errorMsg	= e.getMessage();
			e.printStackTrace();
			return null;
		}
		
		return results;
	}
	
	public boolean isPOInPlant(int purchaseOrderVersionId, int plantId){
		return (elements.get("" + purchaseOrderVersionId + "|" + plantId) != null ? true : false);
	}

	/**
	 * @return the successful
	 */
	public boolean isSuccessful() {
		return successful;
	}

	/**
	 * @return the errorMsg
	 */
	public String getErrorMsg() {
		return errorMsg;
	}
}
